# encoding: utf-8
# @Author: zsl
# @Desc : URL缩短 代码执行失败
# 短网址由于易于记忆和输入，因此在数字营销领域非常受欢迎。这里给大家介绍一下，如何使用Python创建URL缩短器。
# @Date  : 2025/07/08 15:53
from flask import Flask, render_template, request
import requests

app = Flask(__name__)

@app.route('/', methods=['GET', 'POST'])
def index():
    short_url = None
    if request.method == 'POST':
        long_url = request.form['long_url']
        response = requests.post('https://www.googleapis.com/urlshortener/v1/url',
                                 headers={'Content-Type': 'application/json'},
                                 data='{"longUrl": "' + long_url + '"}')
        short_url = response.json()['id']
    return render_template('index.html', short_url=short_url)

if __name__ == '__main__':
    app.run(debug=True)

